package com.zx.systembook.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zx.systembook.model.Seats;
import com.zx.systembook.model.Tables;
import com.zx.systembook.model.TablesMg;
import com.zx.systembook.serviceImp.TableServiceImp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MyController {
    @Autowired
    TableServiceImp tableServiceImp;
    private static Logger  logger = LoggerFactory.getLogger(MyController.class);

    /**
     * 存入数据库
     * @param table
     * @return
     */
    @RequestMapping("save")
    public String Save(@RequestBody String table){
        JSONObject jsonObject = JSON.parseObject(table);
        long table_id = jsonObject.getLongValue("table");
        JSONObject seatInfo=jsonObject.getJSONObject("seatInfo");
        int seatA = (int) seatInfo.getIntValue("seatA");
        int seatB = (int) seatInfo.getIntValue("seatB");
        int seatC = (int) seatInfo.getIntValue("seatC");
        int seatD = (int) seatInfo.getIntValue("seatD");
        TablesMg tablesMg=new TablesMg(table_id,seatA,seatB,seatC,seatD);

        tableServiceImp.update(tablesMg);
        return "table_id: "+table_id+" seatA: "+seatA+" seatB: "+seatB+" seatC: "+seatC+" seatD: "+seatD;
    }

    /**
     * 查询数据
     * @return
     */
    //查询数据
    @RequestMapping("/select")
    public List<Tables> Select(){
         List<TablesMg> list = tableServiceImp.selectAll();
         List<Tables> list1= new ArrayList<>();

         for(int i=0;i<list.size();i++){
             int seatA= list.get(i).getSeata();
             int seatB= list.get(i).getSeatb();
             int seatC= list.get(i).getSeatc();
             int seatD= list.get(i).getSeatd();
             list1.add(new Tables(list.get(i).getId(),new Seats(seatA,seatB,seatC,seatD)));
         }
        return list1;    //jackson自动转换
    }
}
